package mianyang.zhangsan.com.controller;

import mianyang.zhangsan.com.pojo.Admin;
import mianyang.zhangsan.com.pojo.Student;
import mianyang.zhangsan.com.pojo.Teacher;
import mianyang.zhangsan.com.service.AdminService;
import mianyang.zhangsan.com.service.StudentService;
import mianyang.zhangsan.com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;

    /**
     * 用户登录请求
     * @param username 用户名
     * @param password 密码
     * @param  type 身份类型
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password, String type,
                        HttpSession session){
        String message = "";
        if (username.isEmpty()){
            return "{\"success\": false, \"message\": \"用户名不能为空\"}";
        }
        if (password.isEmpty()){
            return "{\"success\": false, \"message\": \"密码不能为空\"}";
        }
        if (type.isEmpty()){
            return "{\"success\": false, \"message\": \"请选择你的身份\"}";
        }
        switch (type){
            case "1" :   //学生身份
                Student student = studentService.queryInfoByUsername(username);
                if (student != null){
                    if (student.getPassword().equals(password)){
                        session.setAttribute("usertype", "1");
                        session.setAttribute("username", student.getUsername());
                        session.setAttribute("mainPage", "welcome");
                        session.setAttribute("user", student);
                        message = "{\"success\": true}";
                    }
                }
                break;
            case "2":   //教师身份
                Teacher teacher = teacherService.queryInfoByUsername(username);
                if (teacher != null){
                    if (teacher.getPassword().equals(password)) {
                        session.setAttribute("usertype", "2");
                        session.setAttribute("username", teacher.getUsername());
                        session.setAttribute("mainPage", "welcome");
                        session.setAttribute("user", teacher);
                        message = "{\"success\": true}";
                    }
                }
                break;
            case "3":  //管理员身份
                Admin admin = adminService.queryInfoByUsername(username);
                if (admin != null){
                    if (admin.getPassword().equals(password)){
                        session.setAttribute("usertype", "3");
                        session.setAttribute("username", admin.getUsername());
                        session.setAttribute("mainPage", "welcome");
                        session.setAttribute("user", admin);
                        message = "{\"success\": true}";
                    }
                }
                break;
        }
        return message;
    }

    /**
     * 退出登录请求
     * @return
     */
    @RequestMapping("/logout")
    public String logout(){
        return "login";
    }

    /**
     * 跳转首页请求
     * @return
     */
    @RequestMapping("/index")
    public String index(){

        return "system/index";
    }

}
